
package cn.lanqiao.ui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.UUID;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import cn.lanqiao.entity.Register;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.Toolkit;
import javax.swing.ImageIcon;

public class RegisterFrm extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel loginuser;
	private JTextField nameField;
	private JTextField accountField;
	private JPasswordField passwordField;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegisterFrm frame = new RegisterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public RegisterFrm() {
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage("photoes\\tubiao.png"));
		setBackground(new Color(255, 255, 255));
		setForeground(new Color(255, 255, 255));
		setTitle("注册");
		setFont(new Font("宋体", Font.PLAIN, 15));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 465, 288);
		loginuser = new JPanel();
		loginuser.setBorder(null);
		loginuser.setBackground(new Color(255, 255, 255));
		setContentPane(loginuser);
		loginuser.setLayout(null);
		
		JLabel namelabel = new JLabel("昵称：");
		namelabel.setForeground(Color.WHITE);
		namelabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		namelabel.setBounds(20, 54, 59, 43);
		loginuser.add(namelabel);
		
		JLabel accountlabel = new JLabel("账号：");
		accountlabel.setForeground(Color.WHITE);
		accountlabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		accountlabel.setBounds(20, 97, 59, 43);
		loginuser.add(accountlabel);
		
		JLabel passwordlabel = new JLabel("密码：");
		passwordlabel.setForeground(Color.WHITE);
		passwordlabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		passwordlabel.setBounds(20, 139, 59, 43);
		loginuser.add(passwordlabel);
		
		nameField = new JTextField();
		nameField.setBounds(92, 66, 267, 21);
		loginuser.add(nameField);
		nameField.setColumns(10);
		
		accountField = new JTextField();
		accountField.setColumns(10);
		accountField.setBounds(92, 108, 267, 21);
		loginuser.add(accountField);
		
		JButton btnRegister = new JButton("");
		btnRegister.setIcon(new ImageIcon("photoes\\zhuce.png"));
		btnRegister.addActionListener(new ActionListener() {
			@SuppressWarnings("unused")
			public void actionPerformed(ActionEvent e) {
				String username = nameField.getText().trim();
				String useraccount = accountField.getText();
				String userpassword = new String (passwordField.getPassword());
				for(int i=0;i<useraccount.length();i++) {
					if(!Character.isDigit(useraccount.charAt(i)) || useraccount.equals("")) {
						JOptionPane.showMessageDialog(null, "账号只能为数字！","输入有误！",JOptionPane.YES_NO_OPTION);
						return;
					}else if(username.equals("") || userpassword.equals("")) {
						JOptionPane.showMessageDialog(null, "输入有误，请检查用户名或密码后重新输入","输入有误！",JOptionPane.YES_NO_OPTION);
						return;
					}else {
						cn.lanqiao.service.RegisterService rservice = new cn.lanqiao.service.impl.RegisterServiceImpl();
						Register register = new Register(UUID.randomUUID().toString(), username, useraccount, userpassword);
							int affter = rservice.Checking(register);
							if(affter>0) {
								JOptionPane.showMessageDialog(null, "注册成功！");
								RegisterFrm.this.setVisible(false);
								UserLoginFrm login = new UserLoginFrm();
								login.setLocationRelativeTo(null);
								login.setVisible(true);
								break;
							}else {
								JOptionPane.showMessageDialog(null, "账号已存在，请重新输入","账号存在！",JOptionPane.YES_NO_OPTION);
								break;
							}
					}
				}
			}
		});
		btnRegister.setFont(new Font("黑体", Font.PLAIN, 24));
		btnRegister.setBounds(92, 191, 267, 58);
		loginuser.add(btnRegister);
		
		JLabel label = new JLabel("别说你没有！");
		label.setFont(new Font("黑体", Font.PLAIN, 14));
		label.setForeground(new Color(0, 255, 255));
		label.setBounds(369, 69, 90, 15);
		loginuser.add(label);
		
		JLabel label_1 = new JLabel("只能填数字！");
		label_1.setFont(new Font("黑体", Font.PLAIN, 14));
		label_1.setForeground(new Color(0, 255, 255));
		label_1.setBounds(369, 111, 90, 15);
		loginuser.add(label_1);
		
		JLabel label_5 = new JLabel("我不会看的！");
		label_5.setFont(new Font("黑体", Font.PLAIN, 14));
		label_5.setForeground(new Color(0, 255, 255));
		label_5.setBounds(369, 153, 90, 15);
		loginuser.add(label_5);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(92, 150, 267, 21);
		loginuser.add(passwordField);
		
		JLabel label_6 = new JLabel("");
		label_6.setIcon(new ImageIcon("photoes\\xiaomao.png"));
		label_6.setBounds(92, 10, 100, 60);
		loginuser.add(label_6);
		
		JLabel label_4 = new JLabel("");
		label_4.setIcon(new ImageIcon("photoes\\richu.png"));
		label_4.setBounds(0, 0, 459, 259);
		loginuser.add(label_4);
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{loginuser, namelabel, accountlabel, passwordlabel, nameField, accountField, passwordField, btnRegister, label, label_1, label_5}));
		
		this.setLocationRelativeTo(null);
	}
}
